import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import DBUtils


class TestLogin:

    def test_login1(self):
        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        # 输入用户名

        driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("lisi")

        #  输入密码
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys("123456")

        # 点击登陆
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()

        time.sleep(2)
        #  获取实际结果
        result = driver.find_element(By.XPATH, '//*[@id="logo"]/a').text  # .text 获取元素的文本数据

        if result == "个人理财系统":
            print("通过")
        else:
            print("不通过！")
            driver.save_screenshot("lisi---123456.png")

        assert result == "个人理财系统"

        driver.quit()


    def test_login2(self):

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        # 输入用户名

        driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("admin")

        #  输入密码
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys("123456")

        # 点击登陆
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()

        time.sleep(2)
        #  获取实际结果
        result = driver.find_element(By.XPATH, '//*[@id="logo"]/a').text  # .text 获取元素的文本数据

        if result == "后台管理系统":
            print("通过")
        else:
            print("不通过！")
            driver.save_screenshot("admin---123456.png")

        assert result == "后台管理系统"

        driver.quit()


    def test_login3(self):

        sql = "insert into user(username,password) values(%s,md5(%s))"
        param1 = ["lisi3","123!@#$%^%&456"]

        DBUtils.update(sql, param1)
        #

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        # 输入用户名

        driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("lisi3")

        #  输入密码
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys("123!@#$%^%&456")

        # 点击登陆
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()

        time.sleep(2)
        #  获取实际结果
        result = driver.find_element(By.XPATH, '//*[@id="logo"]/a').text  # .text 获取元素的文本数据

        if result == "个人理财系统":
            print("通过")
        else:
            print("不通过！")
            driver.save_screenshot("lisi3---123&456.png")

        assert result == "个人理财系统"

        driver.quit()

        sql = "delete from user where username = %s"
        param1 = ["lisi3"]

        DBUtils.update(sql, param1)


    def test_login4(self):

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")
        driver.maximize_window()

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi3")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        result = driver.find_element_by_xpath("//div[@class='login-center1']/form/div[1]/span[1]").text

        if result == '用户名不存在':
            print('通过!')
        else:
            print('不通过！')
            driver.save_screenshot('lisi3-123456.png')

        assert result == '用户名不存在'

        driver.close()


    def test_login5(self):

        driver = webdriver.Chrome()

        driver.get(url='http:localhost:90')

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("1234567")

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        result = driver.find_element_by_xpath("//div[@class='login-center1']/form/div[2]/span[1]").text

        if result == '密码错误':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot(filename='lisi-1234567.png')

        assert result == '密码错误'

        driver.close()


    def test_login6(self):

        driver = webdriver.Chrome()

        driver.get(url='http:localhost:90')

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("admin1")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        result = driver.find_element_by_xpath("//div[@class='login-center1']/form/div[1]/span[1]").text

        if result == '用户名不存在':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot(filename='admin1-123456.png')

        assert result == '用户名不存在'

        driver.close()

    # 测试
    def test_login7(self):

        driver = webdriver.Chrome()

        driver.get(url='http:localhost:90')

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("admin")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("1234567")

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        result = driver.find_element_by_xpath("//div[@class='login-center1']/form/div[2]/span[1]").text

        if result == '密码错误':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot(filename='lisi-1234567.png')

        assert result == '密码错误'

        driver.close()










